package com.vallis.asfc.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vallis.asfc.entity.User;
import com.vallis.asfc.service.UserService;
import com.vallis.asfc.util.JwtUtils;
import com.vallis.asfc.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname LoginController
 * @Description TODO
 * @Date 2020/6/14 19:17
 * @Created by vallis
 */
@RestController

@CrossOrigin
public class LoginController {
    @Autowired
    private UserService userService;
    
    @PostMapping("/login")
    public Result Login(@RequestBody User u) {
        
        
        if (null != u) {
            User e = userService.getOne(new QueryWrapper<User>().eq("user_sn_number", u.getUserName()).eq("user_password", u.getUserPassword()));
    
    
            if (null != e) {
                if (e.getUserDeptId() != 1) {
                    return new Result(400, "该用户无登录权限", null);
                }
                Map<String, Object> stringObjectMap = BeanUtil.beanToMap(e);
                
                String token = JwtUtils.createdToken(e, stringObjectMap);
                System.out.println(token);
                return new Result(200, "登录成功", token);
            }
            return new Result(400, "本系统无此人", null);
        }
        
        
        return new Result(400, "本系统无此人", null);
    }
    
    @GetMapping("/menus")
    public Result getMenus() {
        System.out.println("请求菜单！");
        return new Result(200, "成功", "menus");
    }


}
